import { isNumber, isNumberString } from "./validate";

// 生成唯一随机数
export function getUniqueRandom(): string {
  return `${Date.now()}${Math.random().toString().substring(3, 10)}`;
}

// 转为数字
export function toNumber(num: any): number | undefined {
  if (isNumberString(num)) {
    num = Number.parseFloat(num as string);
  }
  return isNumber(num) ? num : undefined;
}

// 转为整数
export function toInt(num: any): number | undefined {
  if (isNumberString(num)) {
    num = Number.parseFloat(num as string);
  }
  return isNumber(num) ? Math.round(num) : undefined;
}

// 精度转换
export function toFixed(num: number | string, digit = 2): number {
  return Math.round(+num * Math.pow(10, digit)) / Math.pow(10, digit);
}

// 强制去除浮点数
function floatFixed(num: number | string): number {
  if (/[eE]/.test(`${num}`)) {
    const dLen = digitLength(num);
    return dLen > 0 ? Number(num) * Math.pow(10, dLen) : Number(num);
  }
  return Number(`${num}`.replace(".", ""));
}

// 获取小数点长度
function digitLength(num: number | string): number {
  const eSplit = num.toString().split(/[eE]/);
  const len = (eSplit[0].split(".")[1] || "").length - +(eSplit[1] || 0);
  return len > 0 ? len : 0;
}

// 精确计算-加法
export function calcAdd(num1: number | string, num2: number | string): number {
  const accuracy = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
  return (
    (calcMultiply(num1, accuracy) + calcMultiply(num2, accuracy)) / accuracy
  );
}

// 精确计算-减法
export function calcSubtract(
  num1: number | string,
  num2: number | string
): number {
  const accuracy = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
  return (
    (calcMultiply(num1, accuracy) - calcMultiply(num2, accuracy)) / accuracy
  );
}

// 精确计算-乘法
export function calcMultiply(
  num1: number | string,
  num2: number | string
): number {
  const num1Changed = floatFixed(num1);
  const num2Changed = floatFixed(num2);
  const accuracy = digitLength(num1) + digitLength(num2);
  const leftValue = num1Changed * num2Changed;
  return leftValue / Math.pow(10, accuracy);
}

// 精确计算-除法
export function calcDivide(
  num1: number | string,
  num2: number | string
): number {
  const num1Changed = floatFixed(num1);
  const num2Changed = floatFixed(num2);
  return calcMultiply(
    num1Changed / num2Changed,
    Math.pow(10, digitLength(num2) - digitLength(num1))
  );
}

// 米(m)转英尺(ft)
export function meterToFt(num: number, digit = 2): number {
  const ratio = 3.280839895;
  return toFixed((num as number) * ratio, digit);
}

// 英尺(ft)转米(m)
export function ftToMeter(num: number, digit = 2): number {
  const ratio = 3.280839895;
  return toFixed((num as number) / ratio, digit);
}

// 平方米(m²)转平方英尺(ft²)
export function meter2ToFt2(num: number, digit = 2): number {
  const ratio = 10.7639104167;
  return toFixed((num as number) * ratio, digit);
}

// 平方英尺(ft²)转平方米(m²)
export function ft2ToMeter2(num: number, digit = 2): number {
  const ratio = 10.7639104167;
  return toFixed((num as number) / ratio, digit);
}

// 摄氏度(°C)转华氏度(°F)
export function celsiusToFahrenheit(num: number, digit = 0): number {
  return toFixed(((num as number) * 9) / 5 + 32, digit);
}

// 华氏度(°F)转摄氏度(°C)
export function fahrenheitToCelsius(num: number, digit = 0): number {
  return toFixed((((num as number) - 32) * 5) / 9, digit);
}
